﻿Imports Microsoft.Win32

Public Class UtilityClass
    Public Shared Function GetJavaInstallationPath() As String
        Dim javaKey = "SOFTWARE\JavaSoft\Java Runtime Environment"
        Using baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(javaKey)
            Dim currentVersion As String = baseKey.GetValue("CurrentVersion").ToString()
            Using homeKey = baseKey.OpenSubKey(currentVersion)
                Return homeKey.GetValue("JavaHome").ToString()
            End Using
        End Using
    End Function

    Public Shared Function GetJavaManually() As String
        Dim del = New GetJavaManuallyDelegate(AddressOf GetJavaManuallyInvoked)
        Dim x = Application.Current.Dispatcher.Invoke(del)
        Return DirectCast(x, String)
    End Function

    Public Shared Function GetJavaManuallyInvoked() As String
        Dim openfile = New Forms.OpenFileDialog
        openfile.Filter = "java.exe|java.exe"
        Dim res2 = openfile.ShowDialog
        If res2 = Forms.DialogResult.OK OrElse res2 = Forms.DialogResult.Yes Then
            Return openfile.FileName
        End If
        Return ""
    End Function

    Public Delegate Function GetJavaManuallyDelegate() As String
End Class

